/*
 * Copyright ©2015-2023 Jaemon. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloud.alarm.dinger.core.entity.enums;

import com.cloud.alarm.dinger.bytetalk.entity.ByteInteractive;
import com.cloud.alarm.dinger.bytetalk.entity.ByteText;
import com.cloud.alarm.dinger.core.entity.DingerRequest;
import com.cloud.alarm.dinger.core.entity.MsgType;
import com.cloud.alarm.dinger.dingtalk.entity.*;
import com.cloud.alarm.dinger.exception.DingerException;
import com.cloud.alarm.dinger.utils.DingerUtils;
import com.cloud.alarm.dinger.wetalk.entity.WeMarkdown;
import com.cloud.alarm.dinger.wetalk.entity.WeNews;
import com.cloud.alarm.dinger.wetalk.entity.WeText;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.cloud.alarm.dinger.constant.DingerConstant.DINGER_COMMA;
import static com.cloud.alarm.dinger.core.DingerDefinitionHandler.WETALK_AT_ALL;
import static com.cloud.alarm.dinger.core.entity.enums.ExceptionEnum.DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION;

/**
 * 消息体定义子类型
 *
 * @author shuai.zhou
 * @since 1.0
 */
@Getter
@AllArgsConstructor
public enum MessageSubType {

    /**
     * Text类型
     */
    TEXT(true) {
        @Override
        public MsgType msgType(DingerType dingerType, DingerRequest request) {
            String content = request.getContent();
            boolean atAll = request.isAtAll();
            List<String> phones = request.getPhones();
            switch (dingerType) {
                case DINGTALK -> {
                    Message message = new DingText(new DingText.Text(content));
                    if (atAll) {
                        message.setAt(new Message.At(true));
                    } else if (phones != null && !phones.isEmpty()) {
                        message.setAt(new Message.At(phones));
                    }
                    return message;
                }
                case WETALK -> {
                    WeText.Text text = new WeText.Text(content);
                    WeText weText = new WeText(text);
                    if (atAll) {
                        text.setMentioned_mobile_list(Collections.singletonList(WETALK_AT_ALL));
                    } else if (phones != null && !phones.isEmpty()) {
                        text.setMentioned_mobile_list(phones);
                    }
                    return weText;
                }
                case BYTETALK -> {
                    if (atAll) {
                        content = "<at user_id=\"all\">所有人</at>" + content;
                    } else if (!CollectionUtils.isEmpty(phones)) {
                        StringBuilder sb = new StringBuilder();
                        for (String phone : phones) {
                            if (DingerUtils.isEmpty(phone)) {
                                continue;
                            }
                            String[] userIdAndName = phone.split(DINGER_COMMA);
                            if (userIdAndName.length != 2) {
                                continue;
                            }
                            sb.append(String.format("<at user_id=\"%s\">%s</at>", userIdAndName[0], userIdAndName[1]));
                        }
                        content = sb + content;
                    }
                    return new ByteText(new ByteText.Content(content));
                }
                default -> throw new DingerException(DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION, dingerType, this.name());
            }
        }
    },

    /**
     * Markdown类型
     */
    MARKDOWN(true) {
        @Override
        public MsgType msgType(DingerType dingerType, DingerRequest request) {
            String content = request.getContent();
            String title = request.getTitle();
            List<String> phones = request.getPhones();
            switch (dingerType) {
                case DINGTALK -> {
                    Message message = new DingMarkDown(new DingMarkDown.MarkDown(title, content));
                    if (!phones.isEmpty()) {
                        message.setAt(new Message.At(phones));
                    }
                    return message;
                }
                case WETALK -> {
                    WeMarkdown.Markdown markdown = new WeMarkdown.Markdown(content);
                    WeMarkdown weMarkdown = new WeMarkdown(markdown);
                    return weMarkdown;
                }
                default -> throw new DingerException(DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION, dingerType, this.name());
            }
        }
    },

    /**
     * 消息卡片, 只支持 {@link DingerType#BYTETALK}
     */
    INTERACTIVE(true) {
        @Override
        public MsgType msgType(DingerType dingerType, DingerRequest request) {
            String content = request.getContent();
            String title = request.getTitle();
            if (dingerType == DingerType.BYTETALK) {
                return new ByteInteractive(title, content);
            } else {
                throw new DingerException(DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION, dingerType, this.name());
            }
        }
    },

    /**
     * 图文类型
     */
    IMAGETEXT(false) {
        @Override
        public MsgType msgType(DingerType dingerType, DingerRequest request) {
            switch (dingerType) {
                case DINGTALK -> {
                    return new DingFeedCard(new ArrayList<>());
                }
                case WETALK -> {
                    return new WeNews(new ArrayList<>());
                }
                default -> throw new DingerException(DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION, dingerType, this.name());
            }
        }
    },

    /**
     * link类型, 只支持 {@link DingerType#DINGTALK}
     */
    LINK(false) {
        @Override
        public MsgType msgType(DingerType dingerType, DingerRequest request) {
            if (dingerType == DingerType.DINGTALK) {
                return new DingLink();
            } else {
                throw new DingerException(DINGER_UNSUPPORT_MESSAGE_TYPE_EXCEPTION, dingerType, this.name());
            }
        }
    };

    /**
     * 是否支持显示设置消息子类型调用
     */
    private final boolean support;

    /**
     * 获取指定消息类型
     *
     * @param dingerType Dinger类型 {@link DingerType}
     * @param request    消息请求体 {@link  DingerRequest}
     * @return 消息体 {@link MsgType}
     */
    public abstract MsgType msgType(DingerType dingerType, DingerRequest request);

    public static boolean contains(String value) {
        return Arrays.stream(MessageSubType.values()).anyMatch(e -> Objects.equals(e.name(), value));
    }
}